﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using KHMI.Attributes;

namespace DrawTools
{
    [Serializable]
    public class DrawImage : DrawRectangle
    {
        private string imagePath;             //图片路径

        [ExtendCategoryAttribute("扩展设置", VectorName = "图片路径")]
        public string ImagePath
        {
            get { return imagePath; }
            set { imagePath = value; }
        }

		public DrawImage() : this(0, 0, 1, 1)
		{
		}

        public DrawImage(int x, int y, int width, int height)
            : base()
        {
            Rectangle = new Rectangle(x, y, width, height);
            Initialize();
        }

        /// <summary>
        /// Clone this instance
        /// </summary>
        public override DrawObject Clone(DrawObject drawObject)
        {
            base.Clone(drawObject);
            return this;
        }

        // 重载属性保存方法
        public override void SaveToStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
        {
            info.AddValue("imagePath" + orderNumber.ToString(), imagePath);

            base.SaveToStream(info, orderNumber);
        }

        public override void LoadFromStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
        {
            imagePath = info.GetString("imagePath" + orderNumber.ToString());

            base.LoadFromStream(info, orderNumber);
        }

        public override void Draw(Graphics g)
        {           
            try
            {
                Pen pen = new Pen(Color, PenWidth);

                g.DrawImage(Image.FromFile(imagePath),
                            DrawRectangle.GetNormalizedRectangle(Rectangle));

                pen.Dispose();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            
        }
    }
}
